package com.teamone.etl.whitelist.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.enterprise.entity.Enterprise;
import com.teamone.etl.whitelist.dao.WhiteListDao;
import com.teamone.etl.whitelist.entity.WhiteList;
import com.teamone.etl.whitelist.feign.WhiteListFeginService;
import com.teamone.etl.whitelist.service.WhiteListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * IP白名单(WhiteList)表服务实现类
 *
 * @author makejava
 * @since 2021-01-20 11:07:18
 */
@Service("whiteListService")
@Transactional(rollbackFor = Exception.class)
public class WhiteListServiceImpl extends ServiceImpl<WhiteListDao, WhiteList> implements WhiteListService {

    private static final Integer START = 1;
    private static final Integer STOP = 0;
    private static final Integer SIZE = 20;
    @Resource
    private WhiteListDao whiteListDao;
    @Resource
    private WhiteListFeginService whiteListFeginService;

    /**
     * 分页管理列表
     * @param page 当前页
     * @param size 每页显示条数
     * @return 白名单列表
     */
    @Override
    public PageInfo<WhiteList> adminWhiteList(Integer page, Integer size) {
        /*每页查询条数*/
        size = (size > SIZE) ? SIZE : size;
        /*
         * 分页实现
         */
        PageHelper.startPage(page,size);
        List<WhiteList> adminWhiteList = whiteListDao.adminWhiteList();
        return new PageInfo<>(adminWhiteList);
    }
    /**
     * 已启用白名单
     * @return 白名单列表
     */
    @Override
    public PageInfo<WhiteList> userWhiteList(Integer page, Integer size) {
        /*每页查询条数*/
        size = (size > SIZE) ? SIZE : size;
        /*分页实现*/
        PageHelper.startPage(page,size);
        List<WhiteList> userWhiteLisst =  whiteListDao.userWhiteList();
        return  new PageInfo<>(userWhiteLisst);
    }
    /**
     * 根据id删除IP白名单
     * @param id 白名单地址id
     * @return  是否成功
     */
    @Override
    public int deleteWhiteList(Integer id) {
        return whiteListDao.deleteWhiteList(id);
    }

    /**
     * 添加IP白名单
     * @param whiteList 白名单对象
     * @return 是否成功
     */
    @Override
    public int addWhiteList(WhiteList whiteList) {
        /*定义正则表达式*/
        String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        /*根据序号查找对象是否存在*/
        WhiteList whiteListBuyId = whiteListDao.getWhiteListBuyId(whiteList.getId());
        if(whiteListBuyId!=null){
            return STOP;
        }else{
            /*根据传入关联公司名称查找该公司是否存在*/
            Enterprise enterprise = whiteListFeginService.getByEnterpriseName(whiteList.getCompanyName());
            if(enterprise!=null){
                if(whiteList.getIpAddress().matches(regex)) {
                    whiteList.setEnterpriseId(enterprise.getId());
                    whiteList.setUpdateTime(new Date());
                    return whiteListDao.addWhiteList(whiteList);
                }else{
                    return STOP;
                }
            }else{
                return STOP;
            }
        }
    }
    /**
     * 修改白名单
     * @param whiteList 白名单对象
     * @return 是否成功
     */
    @Override
    public int updateWhiteList(WhiteList whiteList) {
        /*定义正则表达式*/
        String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        /*根据传入关联公司名称查找该公司是否存在*/
        Enterprise enterprise = whiteListFeginService.getByEnterpriseName(whiteList.getCompanyName());
        if(enterprise!=null){
            if(whiteList.getIpAddress().matches(regex)){
                whiteList.setEnterpriseId(enterprise.getId());
                whiteList.setUpdateTime(new Date());
                return whiteListDao.updateWhiteList(whiteList);
            }
            return STOP;
        }else{
            return STOP;
        }
    }
    /**
     * 启用IP or 禁用IP
     * @param id 白名单id
     * @return 是否成功
     */
    @Override
    public int updateStatus(Integer id) {
        WhiteList whiteList = whiteListDao.getWhiteListBuyId(id);
        if(whiteList.getStatus().equals(START)){
            return whiteListDao.updateStop(id);
        }else if(whiteList.getStatus().equals(STOP)){
            return whiteListDao.updateStart(id);
        }else{
            return STOP;
        }
    }
    /**
     * 修改IP地址
     * @param whiteList 修改对象
     * @return 是否成功
     */
    @Override
    public int updateIp(WhiteList whiteList) {
        /*定义正则表达式*/
        String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        if(whiteList.getIpAddress().matches(regex)){
            return whiteListDao.updateIp(whiteList);
        }
        return STOP;
    }
    /**
     * 添加多个IP地址
     * @param ipAddressMulti IP地址
     * @return 是否成功
     */
    @Override
    public int addIpAddress(String ipAddressMulti) {
        /*根据返回结果判断是否添加成功*/
        int back = START;
        String ipAddressMulti1 = ipAddressMulti.substring(START,ipAddressMulti.length()-START);
        /*分割ip地址*/
        String[] ipAddress = ipAddressMulti1.split(",");
        /*定义正则表达式*/
        String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." +"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        for (String address : ipAddress) {
            if (address.matches(regex)) {
                WhiteList whiteList = new WhiteList();
                whiteList.setEnterpriseId(null);
                whiteList.setIpAddress(address);
                whiteList.setCreator(null);
                whiteList.setStatus(STOP);
                whiteList.setCreateTime(new Date());
                whiteList.setUpdateTime(new Date());
                /* 判断对象是否添加成功 */
                int reBack = whiteListDao.addIpAddress(whiteList);
                if (reBack != START) {
                    back = STOP;
                }
            }
        }
        return back;
    }

}